package com.example.SearchZB;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.*;
import com.example.SeachZB.R;
import org.json.JSONException;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2014/11/28.
 */
public class Lv2ListActivity extends Activity{

    private Button btn_back3;
    private ListView lv2List;
    private TextView lv2Title;
    private List<Map<String, Object>> serveData;
    private String id;
    private String name;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_lv2list);

        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        name = intent.getStringExtra("name");

        btn_back3 = (Button) findViewById(R.id.btn_back3);
        btn_back3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        lv2List = (ListView) findViewById(R.id.lv2List);
        lv2Title = (TextView) findViewById(R.id.lv2Title);
        lv2Title.setText(name);

        int a = Integer.parseInt(id);
        getlv2Data(a);
    }
    public class lv2adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return serveData.size();
        }

        @Override
        public Object getItem(int position) {
            return serveData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final View view;
            if (convertView == null) {
                view = getLayoutInflater().inflate(R.layout.servelist_item, parent, false);
            } else {
                view = convertView;
            }
            TextView servelist_item = (TextView) view.findViewById(R.id.serveListText);
            servelist_item.setText((String) serveData.get(position).get("name"));
//            servelist_item.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    String name = (String) serveData.get(position).get("name");
//                    Intent intent = new Intent(Lv2ListActivity.this,PoiListActivity.class);
//                    intent.putExtra("name",name);
//                    startActivity(intent);
//                }
//            });
            return view;
        }
    }
    public void getlv2Data(final int n){
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("加载中…");

        AsyncTask<Integer,Integer,Integer> asyncTask = new AsyncTask<Integer, Integer, Integer>() {
            int error = 0;
            @Override
            protected Integer doInBackground(Integer... params) {
                CategoryManager categoryManager = new CategoryManager();
                try {
                    serveData = categoryManager.getCategoryData(n);
                } catch (IOException e) {
                    Log.e("",e.getMessage());
                    error = 1;
                    return error;
                } catch (JSONException e) {
                    error = 2;
                    return error;
                }
                return 0;
            }

            @Override
            protected void onPreExecute() {
                progressDialog.show();
                progressDialog.setMessage("加载中…");
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(Integer integer) {
                if (integer == 1){
                    Toast.makeText(Lv2ListActivity.this, "网络出错，请稍候再试", Toast.LENGTH_SHORT).show();
                }else if (integer == 2){
                    Toast.makeText(Lv2ListActivity.this, "服务器出错，请稍候再试", Toast.LENGTH_SHORT).show();
                }else{
                    lv2adapter lv2adapter = new lv2adapter();
                    lv2List.setAdapter(lv2adapter);
                    progressDialog.dismiss();
                }
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
            }
        };
        asyncTask.execute();
    }
}
